package net.atomtree.util;

import net.atomtree.handler.HTTPRequestPoster;

/**
 * Created by czk on 2015/12/27.
 */
public class RestUtil {
    /**
     * 所有的方法都是拼接url 最终在这里调用restapi 得到json字符串
     * @param url
     * @return
     * @throws Exception
     */
    public static String getJson(String url) throws Exception{
        String json = HTTPRequestPoster.sendGetRequest(url);
        return json;
    }
    /**
     *  /applications
     *  A list of all applications
     *  @param url rest 访问的master地址
     *  @return 返回json格式的application字符串
     *  @throws Exception url访问不到master或worker
     */
    public static String getApplication(StringBuffer url) throws Exception{
        url=url.append("/applications/");
        return getJson(url.toString());
    }
    /**
     * /applications/appname/
     * jobs A list of all jobs for a given application
     * @throws Exception
     */
    public static String getApplicationByappname(StringBuffer url,String appname) throws Exception{
        url=url.append("/applications/").append(appname);
        return getJson(url.toString());
    }
    /**
     *
     * @param url
     * @param appname
     * @return
     * @throws Exception
     */
    public static String getApplicationJob(StringBuffer url,String appname) throws Exception{
        url=url.append("/applications/").append(appname).append("/jobs");
        return getJson(url.toString());
    }
    /**
     * /applications/appname/jobs/[job-appname]
     * Details for the given job
     * @throws Exception
     *
     */
    public static String getApplicationJobByappname(StringBuffer url,String appname,String jobappname) throws Exception{
        url=url.append("/applications/").append(appname).append("/jobs/").append(jobappname);
        return getJson(url.toString());
    }
    /**
     * /applications/appname/stages
     * A list of all stages for a given application
     * @throws Exception
     */
    public static String getApplicationStages(StringBuffer url,String appname) throws Exception{
        System.out.println("进入RestHandler stageUrl未处理之前:"+url);
        url=url.append("/applications/").append(appname).append("/stages");
        System.out.println("进入RestHandler stageUrl未处理之后:"+url);
        return getJson(url.toString());
    }
    /**
     * /applications/appname/stages/[stage-id]
     * A list of all attempts for the given stage
     * @throws Exception
     */
    public static String getApplicationStagesByAppappnameAndStageappname(StringBuffer url,String appname,String stageappname) throws Exception{
        url=url.append("/applications/").append(appname).append("/stages/").append(stageappname);
        return getJson(url.toString());
    }
    /**
     * /applications/appname/stages/[stage-appname]/[stage-attempt-appname]/taskSummary
     * Summary metrics of all tasks in the given stage attempt
     * @throws Exception
     */
    public static String getApplicationStagesTaskSummary(StringBuffer url,String appname,String stageappname,String attemptappname) throws Exception{
        url=url.append("/applications/").append(appname).append("/stages/").append(stageappname).append("/").append(attemptappname).append("/taskSummary");
        return getJson(url.toString());
    }
    /**
     * /applications/appname/stages/[stage-appname]/[stage-attempt-appname]/taskList
     * A list of all tasks for the given stage attempt
     * @throws Exception
     */
    public static String getApplicationStagesTaskList(StringBuffer url,String appname,String stageappname,String attemptappname) throws Exception{
        url=url.append("/applications/").append(appname).append("/stages/").append(stageappname).append("/").append(attemptappname).append("/taskList");
        return getJson(url.toString());
    }
    /**
     * 	/applications/appname/executors
     *  A list of all executors for the given application
     * @throws Exception
     */
    public static String getApplicationExecutor(StringBuffer url,String appname) throws Exception{
        url=url.append("/applications/").append(appname).append("/executors");
        return getJson(url.toString());
    }
    /**
     * 	/applications/appname/storage/rdd
     *  A list of stored RDDs for the given application
     * @throws Exception
     */
    public static String getApplicationRDD(StringBuffer url,String appname) throws Exception{
        url=url.append("/applications/").append(appname).append("/storage/rdd");
        return getJson(url.toString());
    }
    /**
     * 	/applications/appname/storage/rdd/[rdd-appname]
     *  Details for the storage status of a given RDD
     * @throws Exception
     */
    public static String getApplicationRDDByappname(StringBuffer url,String appname,String rddId) throws Exception{
        url=url.append("/applications/").append(appname).append("/storage/rdd/").append(rddId);
        return getJson(url.toString());
    }
}
